package me.zhengjie.mi.neww;


import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import me.zhengjie.mi.old.EasyGmUtils;
import me.zhengjie.utils.DateTimeUtil;
import me.zhengjie.utils.HexConverUtils;
import me.zhengjie.utils.HttpClientUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.hc.core5.http.ParseException;

import java.io.*;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class ttt {

    public static void main(String[] args) throws IOException, ParseException {

        String C = "C010101001010011700BC182A85";
        C = C.substring(0, 15);
        String deptid = "14009";
        String uuid = IdUtil.simpleUUID();
        String last = deptid+uuid;
        String lastitemcode = C+last.toUpperCase();
        lastitemcode = lastitemcode.substring(0, 28);
        System.out.println(C.startsWith("C"));
        System.out.println(C.substring(0, 15));
        System.out.println(lastitemcode);


        String s = "{\"invinfo\":{\"med_list_codg\":\"XN02BBQ128A001010103304\",\"fixmedins_hilist_id\":\"030141\",\"fixmedins_hilist_name\":\"去痛片（索密痛）\",\"rx_flag\":\"1\",\"invdate\":\"2024-01-23\",\"inv_cnt\":0.09,\"manu_lotnum\":\"220936\",\"fixmedins_bchno\":\"10225103014112392517\",\"manu_date\":\"2022-09-27\",\"expy_end\":\"2025-08-31\",\"memo\":\"\"}}";
        JSONObject object = JSONObject.parseObject(s);
        JSONObject object1 = object.getJSONObject("invinfo");
        System.out.println(object);
        System.out.println(object1.get("inv_cnt"));

        String starttime = "2023-05-01";
        String endtime = "2023-10-21";
        Date startDate = DateTimeUtil.parse(starttime, DateTimeUtil.DEFAULT_DATE_FORMAT);
        Date endDate = DateTimeUtil.parse(endtime, DateTimeUtil.DEFAULT_DATE_FORMAT);
        System.out.println(DateTimeUtil.getDateTime(DateTimeUtil.addMonths(endDate, -5), DateTimeUtil.DEFAULT_DATE_FORMAT));
        System.out.println();
        System.out.println(DateTimeUtil.getDateTime(DateTimeUtil.getMonthFirstDay(DateTimeUtil.addMonths(endDate, -5)), DateTimeUtil.DEFAULT_DATE_FORMAT));
        int diffMon = DateTimeUtil.diffMonths(endDate, startDate);
        System.out.println(DateTimeUtil.diffMonths(endDate, startDate));
        Date tempDate = startDate;
        for (int i = 0; i < diffMon; i++) {
            tempDate = DateTimeUtil.addMonths(tempDate, 1);
            System.out.println(DateTimeUtil.getDateTime(tempDate, "yyyy-MM"));
        }

        String datetime = DateUtil.format(DateUtil.date(), DatePattern.PURE_DATETIME_PATTERN);
        long ramdon = (long)Math.floor(Math.random() * 100000);
        String index=String.format("%04d",(1));
        String payItemId = datetime+ramdon+index;
        System.out.println(datetime);
        System.out.println(ramdon);
        System.out.println(index);
        System.out.println(payItemId);
        System.out.println(Long.valueOf(datetime));
        System.out.println(new BigDecimal(payItemId));

        String filePath = "D:\\新建文本文档 - 副本 - 副本.txt";
        JSONObject reqData = new JSONObject();
        StringBuffer stringBuffer = new StringBuffer();
        try {
            BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream(filePath),"UTF-8"));
            String line;
            while ((line = br.readLine()) != null) {
                if (StringUtils.isNoneBlank(line)) {
                    //   reqData = JSONObject.parseObject(line);
                    stringBuffer.append(line);
                }
            }
            br.close();
        } catch (Exception e) {
            System.out.println("读取文件内容出错");
            e.printStackTrace();
        }

        //  System.out.println(new String(stringBuffer.toString().getBytes(), "UTF-8"));
        //System.out.println(HexConverUtils.bytesToHex(stringBuffer.toString().getBytes()));
/*

        JSONArray array = reqData.getJSONArray("list");
        for (int i = 0; i < array.size(); i++) {

            JSONObject o = array.getJSONObject(i);
            System.out.println(o.getString("sort")+"\t"+o.getString("label")+"\t"+o.getString("value")+"\t"+"bus_psn_type"+"\t"+"Y"+"\t"+"0"+"\t"+"admin"+"\t"+"2021-09-30 08:57:33");
        }
*/

        String directoryPath = "D:\\minihis\\2024";
        File directory = new File(directoryPath);
        File[] files = directory.listFiles();

        if (files != null) {
        //    System.out.println(files.length);
            int cnt = 0;
            for (File file : files) {
                if (file.isFile() && file.getName().endsWith("txt")) {
                    cnt++;
                    String filepath = directoryPath+"\\"+file.getName();
                  //  System.out.println(cnt+":\t"+filepath);
                }
            }
        }

        String a1 = "391f0fde-da6e-11eb-b658-0050568f9c84\t2\tC00-D48\t肿瘤\tC30-C39\t呼吸和胸腔内器官恶性肿瘤\tC34\t支气管和肺恶性肿瘤\tC34.9\t未特指的支气管或肺恶性肿瘤\tC34.900x001\t肺恶性肿瘤\t\tnull\tnull\tnull\tnull\tnull\t1\t100000000000202305261620003451890060\t2023-05-26 16:20:00\t2023-05-26 17:30:37\tJ001_20230526000000_A\t2023052600003769\t0\t391f0fde-da6e-11eb-b658-0050568f9c84\tnull\tnull\t";

        String[] arra1 = a1.split("\t");

        for (int i = 0; i < arra1.length; i++) {
             System.out.println((i + 1) +"   -   " + arra1[i]);
        }
//T000100141	T000100141	醋北柴胡	醋北柴胡	3～10g。	辛、苦,微寒。归肝、胆、肺经。	干燥根	疏散退热,疏肝解郁,升举阳气。用于感冒发热,寒热往来,胸胁胀痛,月经不调,子宫脱垂,脱肛。	取北柴胡片,照醋炙法炒干。	解表药	柴胡	基金予以支付
        stringBuffer
                .append(arra1[0]).append("\t")  // 医疗目录编码(drugcode)
                .append(arra1[0]).append("\t")  // 医疗目录编码(drugcountrycode)
                .append(arra1[1]).append("\t")  // 单味药名称(tradename)
                .append(arra1[1]).append("\t")  // 单味药名称(regularname)
                .append(arra1[7]).append("\t")  // 常规用法(specs)
                .append(arra1[8]).append("\t")  // 性味(dosemodel)
                .append(arra1[5]).append("\t")  // 药用部位(medpart)
                .append(arra1[19]).append("\t")  // 药材名称(medname)
                .append(arra1[20]).append("\t")  // 功能主治(medindications)
                .append(arra1[21]).append("\t")  // 炮制方法(medprosmethod)
                .append(arra1[22]).append("\t")  // 功效分类(medeffect)
                .append(arra1[23]).append("\t"); // 药材种来源(medsource)
        System.out.println(stringBuffer.toString());

        // System.out.println("220202198906063333".substring(6, 14));

        String[] a = "20230131183843-1ZC2-5721^202301311837100001ZC2742^1|4166|0|4166|0|0|0|0|0|0|0|0|0|833.2|0|0|833.2|0|0|72|11|0|0|0|3332.8|833.2|0|0|0|0|0|0|0|0|1ZC2|2023010912582547|A2023010912582519|84|0|0|0|0|833.2(20%)|0|0|0|0|0|0||^".split("\\|");


    }
    /**
     * 计算地球上任意两点(经纬度)距离
     *
     * @param long1 第一点经度
     * @param lat1  第一点纬度
     * @param long2 第二点经度
     * @param lat2  第二点纬度
     * @return 返回距离 单位：米
     */
    public static Map<String, String> distanceByLongNLat(double long1, double lat1, double long2, double lat2) {
        double a, b, R;
        R = 6378137;//地球半径
        lat1 = lat1 * Math.PI / 180.0;
        lat2 = lat2 * Math.PI / 180.0;
        a = lat1 - lat2;
        b = (long1 - long2) * Math.PI / 180.0;
        double d;
        double sa2, sb2;
        sa2 = Math.sin(a / 2.0);
        sb2 = Math.sin(b / 2.0);
        d = 2 * R * Math.asin(Math.sqrt(sa2 * sa2 + Math.cos(lat1) * Math.cos(lat2) * sb2 * sb2));

        BigDecimal meter = new BigDecimal(d).setScale(2, RoundingMode.UP);
        BigDecimal kilometer = meter.multiply(new BigDecimal("0.001")).setScale(2, RoundingMode.UP);

        Map<String, String> map = new HashMap<>();
        map.put("meter", meter.toPlainString());
        map.put("kilometer", kilometer.toPlainString());

        return map;
    }

    public static void unzip(String zipFilePath, String destDir) throws IOException {
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(zipFilePath));
        ZipInputStream zis = new ZipInputStream(bis);

        byte[] buffer = new byte[1024];
        ZipEntry zipEntry;

        while ((zipEntry = zis.getNextEntry()) != null) {
            String entryName = zipEntry.getName();
            File destFile = new File(destDir);

            if (!zipEntry.isDirectory()) {
                destFile.getParentFile().mkdirs();

                FileOutputStream fos = new FileOutputStream(destFile);
                BufferedOutputStream bos = new BufferedOutputStream(fos);

                int bytesRead;
                while ((bytesRead = zis.read(buffer)) != -1) {
                    bos.write(buffer, 0, bytesRead);
                }

                bos.close();
                fos.close();
            } else {
                destFile.mkdirs();
            }
        }

        zis.close();
        bis.close();
    }
    public static boolean isRareCharacter(char c) {
        int codePoint = Character.codePointAt(new char[]{c}, 0);
        return codePoint >= 0x20000 && codePoint <= 0x2A6DF; // 扩展B区
    }
}
